<template>
    <div>
        <!-- 表格 -->
        <el-table style="width: 100%;" border :data="records">
            <el-table-column label="序号" type="index" width="60px" align="center"></el-table-column>
            <el-table-column label="名称" prop="skuName"></el-table-column>
            <el-table-column label="描述" prop="skuDesc"></el-table-column>
            <el-table-column label="默认图片" width="130px">
                <template slot-scope="{row,index}">
                    <img :src="row.skuDefaultImg" alt="图裂开了" style="width: 85px; height: 85px;">
                </template>
            </el-table-column>
            <el-table-column label="重量" prop="weight" width="60px"></el-table-column>
            <el-table-column label="价格" prop="price" width="60px"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="{row,$index}">
                    <el-button type="success" icon="el-icon-bottom" size="mini" v-if="row.isSale==0" @click="sale(row)">
                    </el-button>
                    <el-button type="success" icon="el-icon-top" size="mini" v-else @click="cancel(row)"></el-button>
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit"></el-button>
                    <el-button type="info" icon="el-icon-info" size="mini" @click="getSkuInfo(row)"></el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteSku(row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination style="text-align: center;" :current-page="page" :page-sizes="[3, 5, 10, 100]" :page-size="limit"
            @current-change="getSkulist" @size-change="handleSizeChange"
            layout="prev, pager, next, jumper,->,sizes,total" :total="total">
        </el-pagination>
        <!-- 抽屉功能 -->
        <el-drawer :visible.sync="drawer" :show-close='false' size="50%">
            <el-row>
                <el-col :span="5">
                    名称
                </el-col>
                <el-col :span="16">
                    {{skuInfo.skuName}}
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="5">
                    描述
                </el-col>
                <el-col :span="16">
                    {{skuInfo.skuDesc}}
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="5">
                    价格
                </el-col>
                <el-col :span="16">
                    {{skuInfo.price}}元
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="5">
                    平台属性
                </el-col>
                <el-col :span="16">
                    <template>
                        <el-tag type="success" v-for="(attr,index) in skuInfo.skuAttrValueList" :key="attr.id">
                            {{attr.attrId}}-{{attr.valueId}}
                        </el-tag>
                    </template>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="5">
                    商品图片
                </el-col>
                <el-col :span="16">
                    <el-carousel height="500px">
                        <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                            <img :src="item.imgUrl" alt="" style="width: 100%; height: 100%;">
                        </el-carousel-item>
                    </el-carousel>
                </el-col>
            </el-row>
        </el-drawer>
    </div>
</template>
<script>
    export default {
        name: 'Sku',
        data() {
            return {
                page: 1,
                limit: 10,
                // 存储SKU列表的数据
                records: [],
                total: 0,
                skuInfo: {},
                drawer: false
            }
        },
        mounted() {
            // 获取SKU列表的方法
            this.getSkulist()
        },
        methods: {
            //获取SKU数据列表的方法
            async getSkulist(pages = 1) {
                this.page = pages
                const { page, limit } = this
                let res = await this.$API.sku.reqSkulist(page, limit)
                if (res.code == 200) {
                    this.records = res.data.records
                    this.total = res.data.total
                }
            },
            handleSizeChange(limit) {
                this.limit = limit
                this.getSkulist()
            },
            //上架
            async sale(row) {
                let res = await this.$API.sku.reqSale(row.id)
                if (res.code == 200) {
                    this.$message({ type: 'success', message: '上架成功' })
                    row.isSale = 1
                }
            },
            //下架
            async cancel(row) {
                let res = await this.$API.sku.reqcancelSale(row.id)
                if (res.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '下架成功'
                    })
                    row.isSale = 0
                }
            },
            edit() {
                this.$message({
                    type: 'info',
                    message: "正在开发中。。。。。"
                })
            },
            // 获取SKU详情的回调
            async getSkuInfo(row) {
                this.drawer = true
                let res = await this.$API.sku.reqSkuById(row.id)
                this.skuInfo = res.data
            },
            async deleteSku(row) {

                let res = await this.$API.sku.reqDeleteSku(row.id)
                if (res.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '删除成功'
                    })
                    this.getSkulist(this.records.length > 1 ? this.page : this.page - 1)
                }
            }
        },
    }
</script>
<style scoped>
    .el-row .el-col-5 {
        font-size: 18px;
        text-align: right;
    }

    .el-col {
        margin: 10px 10px;
    }
</style>
<style>
    .el-carousel__item h3 {
        color: #475669;
        font-size: 14px;
        opacity: 0.75;
        line-height: 150px;
        margin: 0;
    }

    .el-carousel__item:nth-child(2n) {
        background-color: #99a9bf;
    }

    .el-carousel__item:nth-child(2n+1) {
        background-color: #d3dce6;
    }

    .el-carousel__button {
        background-color: #ff0000;
        height: 10px;
        width: 10px;
        border-radius: 50%;
    }
</style>